package mapEditor.view
{
	import asset.mapEditor.topMenu;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mapEditor.common.DialogBoxManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.EditorModel;
	import mapEditor.vo.ToolType;
	
	public class TopMenuView extends topMenu
	{
		private var _model:EditorModel;
		private var _createMapView:CreateMapView;
		private var _stage:Stage;
			
		public function TopMenuView(stage:Stage, model:EditorModel)
		{
			_stage=stage;
			_model=model;
			toolNew.buttonMode=toolBlock.buttonMode=toolClearBlock.buttonMode=toolOpen.buttonMode=toolSave.buttonMode=toolTest.buttonMode=toolMapAttrib.buttonMode=true;
			
			setEvent();
		}
		
		private function setEvent():void
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddStage);
			addEventListener(MouseEvent.MOUSE_OVER, onToolMouseOver);
			toolNew.addEventListener(MouseEvent.CLICK, onToolClick);
			toolOpen.addEventListener(MouseEvent.CLICK, onToolClick);			
			toolSave.addEventListener(MouseEvent.CLICK, onToolClick);
			toolBlock.addEventListener(MouseEvent.CLICK, onToolClick);
			toolClearBlock.addEventListener(MouseEvent.CLICK, onToolClick);
			toolTest.addEventListener(MouseEvent.CLICK, onToolClick);
			toolMapAttrib.addEventListener(MouseEvent.CLICK, onToolClick);
		}
		
		private function removeEvent():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddStage);
			removeEventListener(MouseEvent.MOUSE_OVER, onToolMouseOver);
			toolNew.removeEventListener(MouseEvent.CLICK, onToolClick);
			toolOpen.removeEventListener(MouseEvent.CLICK, onToolClick);			
			toolSave.removeEventListener(MouseEvent.CLICK, onToolClick);
			toolBlock.removeEventListener(MouseEvent.CLICK, onToolClick);
			toolClearBlock.removeEventListener(MouseEvent.CLICK, onToolClick);
			toolTest.removeEventListener(MouseEvent.CLICK, onToolClick);
			toolMapAttrib.removeEventListener(MouseEvent.CLICK, onToolClick);
			if(_stage) _stage.removeEventListener(Event.RESIZE, onReSize);
		}
		
		private function __onMapNewCreate(evt:MapEditorEvent):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_NEW_CREATE));
		}
		
		private function onToolClick(evt:MouseEvent):void
		{
			switch(evt.currentTarget)
			{
				case toolNew://新建地图
					createMap();
					break;
				case toolOpen://打开地图
					_model.toolType=ToolType.OPEN_MAP;
					break;				
				case toolBlock://设置障碍点
					_model.toolType=ToolType.ROAD_BLOCK;
					break;
				case toolClearBlock://清除障碍点
					_model.toolType=ToolType.CLEAR_ROAD_BLOCK;
					break;
				case toolSave://保存
					_model.toolType=ToolType.SAVE_MAP;
					break;
				case toolTest://测试地图
					_model.toolType=ToolType.TEST_MAP;
					break;
				case toolMapAttrib://地图属性
					_model.toolType=ToolType.MAP_ATTRIB;
					break;				
			}
		}
		
		private function onAddStage(evt:Event):void
		{
			_stage.addEventListener(Event.RESIZE, onReSize);
		}
		
		private function onReSize(evt:Event):void
		{
			toolBg.width=_stage.stageWidth+10;
		}
		
		private function onToolMouseOver(evt:MouseEvent):void
		{
			Mouse.show();
			Mouse.cursor=MouseCursor.AUTO;
		}
		
		/**
		 *新建地图 
		 */		
		private function createMap():void
		{
			if(_createMapView)
			{
				if(_createMapView.parent) _createMapView.parent.removeChild(_createMapView);
				_createMapView.dispose();
			}
			_createMapView=null;
			
			_createMapView=new CreateMapView(_model);
			_createMapView.addEventListener(MapEditorEvent.MAP_NEW_CREATE, __onMapNewCreate);
			DialogBoxManager.Instance.show(_createMapView, true, false);
		}
		
		public function dispose():void
		{
			removeEvent();
			if(_createMapView)
			{
				if(_createMapView.parent) _createMapView.parent.removeChild(_createMapView);
				_createMapView.dispose();
			}
			_createMapView=null;
		}
	}
}